<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2020/10/13
 * Time: 11:43 AM
 */
namespace app\model;


use think\facade\Db;
use think\Model;

class Organization extends Model{
    protected $table = 'organization';
    protected $pk = 'id';

    protected static function init(){
        //TODO:初始化内容
    }

    private function _getOutField(){
        return [
            'id' => 'orgid',
            'orgname' => 'orgname',
            'oreaddress' => 'oreaddress',
            'ispersonnel' => 'ispersonnel',
            'isphoto' => 'isphoto',
            'floor' => 'floor',
            'orgstatus' => 'orgstatus',
            'createtime' => 'createtime',
        ];
    }
    public function getOrgListByCondition($where = '', $order = 'id desc', $field = ''){

        $field = $field ?: self::_getOutField();
        $list = Db::table('organization')
            ->where($where)
            ->field($field)
            ->order($order)
            ->paginate(input('param.pageSize'))
            ->render();

        return $list;
    }

    public function getOrgListByOrgId($orgId = 0){

        $where = [
            'status' => 1
        ];
        if($orgId > 0){
            $where['id'] = $orgId;
        }

        return Db::table('organization')
            ->where($where)
            ->field('id as orgid,orgname')
            ->select();

    }


    public function addOneOrg($data){
        $data['createtime'] = time();
        return Db::table('organization')->insertGetId($data);

    }
    public function getOrgInfoById($id,$field = '*'){
        return Db::table('organization')->where(['id'=>$id])->field($field)->find();
    }

    public function updateOrgInfoByWhere($where,$data){

        $data['updatetime'] = time();
        return Db::table('organization')->where($where)->update($data);
    }
}